<?php
class OrderController extends AppController {

	public $name = 'order';
	function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array('index','editdate','doeditdate','getareas','orderview','ordershow','setstatus');

	}
	
	public function index() {	
        $page = Request::get_args('page');	
        $payid = Request::get_args('payid');
        $pay_status = intval(Request::get_args('pay_status'));
        $transport_status = intval(Request::get_args('transport_status'));
        $pay_id = intval(Request::get_args('pay_id'));
        $order_status = intval(Request::get_args('order_status'));	
        $conditions = array();
        if($payid) {
		    $conditions['orderinfo.payid like'] = "%$payid%";
	    }
        if($pay_status) {
		    $conditions['orderinfo.pay_status'] = 1;		
	    }
        if($transport_status) {
		    $conditions['orderinfo.transport_status'] = 1;		
	    }
        if($pay_id) {
		    $conditions['orderinfo.pay_id'] = $pay_id;			
	    }		
        if($order_status) {
		    $conditions['orderinfo.order_status'] = $order_status;			
	    }
		
        $url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $tb = array('model' => 'orderinfo',
 		            'joins' => array(
					    array('type'=>'left','table'=>'shopinfo','conditions'=>array('orderinfo.shop_id' =>'shopinfo.id')),					
						),
				    'fields' => array('orderinfo.id','orderinfo.payid','orderinfo.shop_id','orderinfo.transport_price','
			orderinfo.order_amount','orderinfo.pay_name','orderinfo.order_time','orderinfo.order_status','orderinfo.protect_status','orderinfo.pay_status','orderinfo.transport_status','shopinfo.shop_name'),
					'conditions' => $conditions,
					'order' => ' orderinfo.order_time desc ',
					'limit' => '10',
					'group' => '',
					'url' => preg_replace("/&page=([^&]+)/","",$url_this),
					'page' => $page,
					'plist' => 6				
				); 
	    $result =  $this->fpage($tb); 
		
		$query = array(
			'fields' => array('payment.*'), 
		);
		$array = POP::exe('[@@:UM payment][@@:QU find:all]',$query);			
	    $payment = array();
	    foreach($array as $value) {
		    $payment[$value['payment']['id']] = $value['payment'];
	    }		
        $this->set('result',$result);
        $this->set('payment',$payment);	

        $this->set('payid',$payid);	
        $this->set('pay_status',$pay_status);	
        $this->set('transport_status',$transport_status);	
        $this->set('pay_id',$pay_id);	
        $this->set('order_status',$order_status);			
	
	}
	
	public function ordershow() {	
        $order_id = intval(Request::get_args('id'));
        if(!$order_id) { 
		    $this->flash("参数错误！", Request::referer()); 
		}
	    $user_id="";		
		$info  = array();		
		$query = array(
			'conditions' => array('orderinfo.id' => $order_id), 
			'fields' => array('orderinfo.*'), 
		);
		$info = POP::exe('[@@:UM orderinfo][@@:QU find:first]',$query);			
		if(!$info ) { exit('该订单不存在！');  }
		$info = $info['orderinfo'];
		$query = array(
 		            'joins' => array(
					    array('type'=>'left','table'=>'goods','conditions'=>array('ordergoods.goods_id' =>'goods.id')),	
					    array('type'=>'left','table'=>'shopinfo','conditions'=>array('ordergoods.shop_id' =>'shopinfo.id')),						
						),
				    'fields' => array('ordergoods.goods_id','ordergoods.goods_price','ordergoods.order_num','ordergoods.goods_name','ordergoods.id','ordergoods.good_tran','
		goods.goods_thumb','shopinfo.user_id','shopinfo.shop_name'),
					'conditions' => array('ordergoods.order_id'=>$info['id']),
				);	
	    $list = POP::exe('[@@:UM ordergoods][@@:QU find:all]',$query);
	    $info['order_goods']=$list;		
		$query = array(
			'fields' => array('area.*'), 
		);
		$return_array_area = POP::exe('[@@:UM area][@@:QU find:all]',$query);
		
	    $areas = array();		
	    foreach($return_array_area as $k=>$v) {
		    $areas[$v['area']['id']] = $v['area']['area_name'];
	    }
		
		$query = array(
			'conditions' => array('shoppayment.shop_id' => $info['shop_id']), 
			'fields' => array('shoppayment.*'), 
		);
		$payment_info = POP::exe('[@@:UM shoppayment][@@:QU find:all]',$query);			

	    $shop_payment = array();
	    if($payment_info) {
		    foreach($payment_info as $info1) {
			    $temp = unserialize($info1['shoppayment']['pay_config']);
			    if(is_array($temp)){
				    $info1 = array_merge($info1, $temp);
			    }
			    $shop_payment[$info1['shoppayment']['id']] = $info1;
		    }
	    }  
		$query = array(
			'conditions' => array('shopinfo.id' => $info['shop_id']), 
			'fields' => array('shopinfo.*'), 
		);
		$shop_info = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);	
	    $shop_info = $shop_info['shopinfo'];
	    $info['shop_name'] = $shop_info['shop_name'];
		

		$query = array(
			'conditions' => array('payment.enabled' => 1), 
			'fields' => array('payment.*'), 
		);
		$array = POP::exe('[@@:UM payment][@@:QU find:all]',$query);	

	    $payarr = array();
	    foreach($array as $value) {
		    $payarr[$value['payment']['id']] = $value['payment'];
	    }	

		$query = array(
			'conditions' => array('shopcategories.parent_id' => 0), 
			'fields' => array('shopcategories.*'), 
		);
		$shop_categories_parent = POP::exe('[@@:UM shopcategories][@@:QU find:all]',$query);
	
	    $areas_info = array();		
	    foreach($return_array_area as $k=>$v) {
		    $areas_info[$v['area']['area_type']][$v['area']['id']]['id'] = $v['area']['id'];
		    $areas_info[$v['area']['area_type']][$v['area']['id']]['parent_id'] = $v['area']['parent_id'];
		    $areas_info[$v['area']['area_type']][$v['area']['id']]['area_name'] = $v['area']['area_name'];
		    $areas_info[$v['area']['area_type']][$v['area']['id']]['area_type'] = $v['area']['area_type'];
	    }	
        $categories_parent = 0;
        $categories_child = 0;	
        if(isset($shop_info['shop_categories'])){
			$query = array(
				'conditions' => array('shopcategories.id' => $shop_info['shop_categories']), 
				'fields' => array('shopcategories.*'), 
			);
			$shop_categories_info = POP::exe('[@@:UM shopcategories][@@:QU find:first]',$query);		
            $shop_categories_info = $shop_categories_info['shopcategories'];
	        if($shop_categories_info['parent_id'] == 0){
		        $categories_parent = $shop_info['shop_categories'];
	        } else {
		        $categories_parent = $shop_categories_info['parent_id'];
		        $categories_child = $shop_info['shop_categories'];
	        }
        }	

		$this->set('info',$info);	
		$this->set('areas_info',$areas_info);
		$this->set('areas',$areas);		
		$this->set('payarr',$payarr);
		$this->set('shop_info',$shop_info);
		
	}
	
	public function doeditdate() {
        //定义两个数组，分别以站内信的形式将修改状态发送给买卖双方
        $array = array();
        $shoparr = array();
        $nowtime = date("Y-m-d H:i:s");
        $order_id = intval(Request::get_args('order_id'));
        $order_value = intval(Request::get_args('order_value'));
        $user_id="";				
		$info  = array();
		$query = array(
			'conditions' => array('orderinfo.id' => $order_id), 
			'fields' => array('orderinfo.*'), 
		);
		$info = POP::exe('[@@:UM orderinfo][@@:QU find:first]',$query);	
		
		if(!$info ) { exit('该订单不存在！');  }
		$info = $info['orderinfo'];
		$query = array(
 		            'joins' => array(
					    array('type'=>'left','table'=>'goods','conditions'=>array('ordergoods.goods_id' =>'goods.id')),	
					    array('type'=>'left','table'=>'shopinfo','conditions'=>array('ordergoods.shop_id' =>'shopinfo.id')),						
						),
				    'fields' => array('ordergoods.goods_id','ordergoods.goods_price','ordergoods.order_num','ordergoods.goods_name','ordergoods.id','ordergoods.good_tran','
		goods.goods_thumb','shopinfo.user_id','shopinfo.shop_name'),
					'conditions' => array('ordergoods.order_id'=>$info['id']),
				);	
	    $list = POP::exe('[@@:UM ordergoods][@@:QU find:first]',$query);		

	    $info['order_goods']=$list;
        if($info['order_value']!=$order_value){
	        //买方发送消息
	        $maiarray['user_id'] = $info['user_id'];
	        $maiarray['remind_info'] = "在".$nowtime."，"."管理员将订单金额修改，订单号为：".$info['payid'];
	        $maiarray['remind_time'] = $nowtime;
	        //卖方发送消息
	        $shoparr['user_id'] = $info['shop_id'];
	        $shoparr['remind_info'] = "在".$nowtime."，"."管理员将订单金额修改，订单号为：".$info['payid'];
	        $shoparr['remind_time'] = $nowtime;
        }
        $post['order_value']=intval(Request::get_args('order_value'));
        $post['consignee']=Request::get_args('consignee');
        $post['mobile']=Request::get_args('mobile');
        $post['telphone']=Request::get_args('telphone');
        $post['email']=Request::get_args('email');
        $post['country']=Request::get_args('country');
        $post['province']=Request::get_args('province');
        $post['city']=Request::get_args('city');
        $post['district']=Request::get_args('district');
        $post['address']=Request::get_args('address');
        $post['zipcode']=Request::get_args('zipcode');
        $post['shipping_name']=Request::get_args('shipping_name');
        $post['shipping_no']=Request::get_args('shipping_no');
        $post['shipping_type']=Request::get_args('shipping_type');
        $post['pay_name']=Request::get_args('pay_name');		
	    $safeData['orderinfo'] = $post;	
		$query['keyid'] = $order_id;
		$query['set']= $safeData;		
		if (POP::exe('[@@:UM orderinfo][@@:QU save]',$query)) {		
		    //修改订单状态;
			$array = array(
				'log_time' => date("Y-m-d H:i:s"),
				'admin_id' => $this->my->id,
			    'admin_name' => $this->my->username,
				'log_info' => "订单被修改，订单号为:".$order_id,
			    'ip_address' => Request::getclientIp(),					
			);
			$safeData['adminlog'] = $array;									
			POP::exe('[@@:UM adminlog][@@:QU save]',$safeData);				

	        if($info['order_value']!=$order_value){
			    //发送给买方
				$safeData['remindinfo'] = $maiarray;								
				POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);
				//发送给卖方
				$safeData['remindinfo'] = $shoparr;								
				POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);
	        }
	        $this->flash("修改成功！", Router::url('admin/order/editdate?id='.$order_id));
        } else {
	        $this->flash("修改失败！", Router::url('admin/order/editdate?id='.$order_id));
        }		

    }	
	
	public function editdate() {
        $order_id = intval(Request::get_args('id'));
        if(!$order_id) { 
		    $this->flash("参数错误！", Request::referer()); 
		}
	    $user_id="";
		$info  = array();
		$query = array(
			'conditions' => array('orderinfo.id' => $order_id), 
			'fields' => array('orderinfo.*'), 
		);
		$info = POP::exe('[@@:UM orderinfo][@@:QU find:first]',$query);					
		if(!$info ) { exit('该订单不存在！');  }
		$info = $info['orderinfo'];
		$query = array(
 		            'joins' => array(
					    array('type'=>'left','table'=>'goods','conditions'=>array('ordergoods.goods_id' =>'goods.id')),	
					    array('type'=>'left','table'=>'shopinfo','conditions'=>array('ordergoods.shop_id' =>'shopinfo.id')),						
						),
				    'fields' => array('ordergoods.goods_id','ordergoods.goods_price','ordergoods.order_num','ordergoods.goods_name','ordergoods.id','ordergoods.good_tran','
		goods.goods_thumb','shopinfo.user_id','shopinfo.shop_name'),
					'conditions' => array('ordergoods.order_id'=>$info['id']),
				);	
	    $list = POP::exe('[@@:UM ordergoods][@@:QU find:all]',$query);		
	    $info['order_goods']=$list;
		$query = array(
			'fields' => array('area.*'), 
		);
		$return_array_area = POP::exe('[@@:UM area][@@:QU find:all]',$query);		
	    $areas = array();		
	    foreach($return_array_area as $k=>$v) {
		    $areas[$v['area']['id']] = $v['area']['area_name'];
	    }
		$query = array(
			'conditions' => array('shoppayment.shop_id' => $info['shop_id']), 
			'fields' => array('shoppayment.*'), 
		);
		$payment_info = POP::exe('[@@:UM shoppayment][@@:QU find:all]',$query);			
	    $shop_payment = array();
	    if($payment_info) {
		    foreach($payment_info as $info1) {
			    $temp = unserialize($info1['shoppayment']['pay_config']);
			    if(is_array($temp)){
				    $info1 = array_merge($info1, $temp);
			    }
			    $shop_payment[$info1['shoppayment']['id']] = $info1;
		    }
	    }    
		$query = array(
			'conditions' => array('shopinfo.id' => $info['shop_id']), 
			'fields' => array('shopinfo.*'), 
		);
		$shop_info = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);	
	
	    $shop_info = $shop_info['shopinfo'];
	    $info['shop_name'] = $shop_info['shop_name'];
		
		$query = array(
			'conditions' => array('payment.enabled' => 1), 
			'fields' => array('payment.*'), 
		);
		$array = POP::exe('[@@:UM payment][@@:QU find:all]',$query);		

	    $payarr = array();
	    foreach($array as $value) {
		    $payarr[$value['payment']['id']] = $value['payment'];
	    }	
		$query = array(
			'conditions' => array('shopcategories.parent_id' => 0), 
			'fields' => array('shopcategories.*'), 
		);
		$shop_categories_parent = POP::exe('[@@:UM shopcategories][@@:QU find:all]',$query);	
	    $areas_info = array();		
	    foreach($return_array_area as $k=>$v) {
		    $areas_info[$v['area']['area_type']][$v['area']['id']]['id'] = $v['area']['id'];
		    $areas_info[$v['area']['area_type']][$v['area']['id']]['parent_id'] = $v['area']['parent_id'];
		    $areas_info[$v['area']['area_type']][$v['area']['id']]['area_name'] = $v['area']['area_name'];
		    $areas_info[$v['area']['area_type']][$v['area']['id']]['area_type'] = $v['area']['area_type'];
	    }	
        $categories_parent = 0;
        $categories_child = 0;	
        if(isset($shop_info['shop_categories'])){
			$query = array(
				'conditions' => array('shopcategories.id' => $shop_info['shop_categories']), 
				'fields' => array('shopcategories.*'), 
			);
			$shop_categories_info = POP::exe('[@@:UM shopcategories][@@:QU find:first]',$query);		
            $shop_categories_info = $shop_categories_info['shopcategories'];
	        if($shop_categories_info['parent_id'] == 0){
		        $categories_parent = $shop_info['shop_categories'];
	        } else {
		        $categories_parent = $shop_categories_info['parent_id'];
		        $categories_child = $shop_info['shop_categories'];
	        }
        }	

		$this->set('info',$info);	
		$this->set('areas_info',$areas_info);	
		$this->set('payarr',$payarr);
		$this->set('shop_info',$shop_info);
		
    }	
	
	public function getareas() {
	
        $parent_id = intval($_POST['value']);
        $type = intval($_POST['type']);
        $mode = $_POST['mode'];
        $pre = $_POST['pre'];		
        if(!$parent_id) {
	        exit();
        }
        if($type<0 || $type>2 || empty($mode)) {
	        exit();
        }	
		$query = array(
			'conditions' => array('area.parent_id' => $parent_id), 
			'fields' => array('area.*'), 
		);
		$areas_info = POP::exe('[@@:UM area][@@:QU find:all]',$query);			
        header('Content-Type: text/html; charset='.Param::read('App.encoding'));		
        $select = "";
        if($type==0) {
	        $select .= '<select name="data['.$mode.']['.$pre.'province]"   onchange="areachanged(this.value,1);">';
	        $select .= '<option value="0">请选择省份...</option>';
        } elseif($type==1) {
	        $select .= '<select name="data['.$mode.']['.$pre.'city]"   onchange="areachanged(this.value,2);">';
	        $select .= '<option value="0">请选择城市...</option>';
        } elseif($type==2) {
	        $select .= '<select name="data['.$mode.']['.$pre.'district]">';
	        $select .= '<option value="0">请选择区域...</option>';
        }
        foreach($areas_info as $v) {
	        $select .= '<option value="' . $v['area']['id'] . '">' . $v['area']['area_name'] . '</option>';
        } 
        $select .= '</select>';
        echo $select;
		exit;

	}	
	
	public function orderview() {	
        /* post */
        $order_id = intval(Request::get_args('order_id'));
        $state = intval(Request::get_args('state'));
		$row  = array();
		$query = array(
			'conditions' => array('orderinfo.id' => $order_id), 
			'fields' => array('orderinfo.*'), 
		);
		$row = POP::exe('[@@:UM orderinfo][@@:QU find:first]',$query);				
		if(!$row ) { exit('该订单不存在！');  }
		$row = $row['orderinfo'];
        $nowtime = date("Y-m-d H:i:s");
        $maiarray = array();
        $post = array();
		$setdata = array();
        /* 取消订单*/
        if($state==1){
		    $setdata['order_status'] = 0;
	        $sn = '取消订单，订单id为'.$order_id;
	        $maiarray['user_id'] = $row['user_id'];
	        $maiarray['remind_info'] = '在'.$nowtime."，"."管理员给您取消了订单，订单号为:".$row['payid'];
	        $maiarray['remind_time'] = $nowtime;
	
	        $post['user_id'] = $row['shop_id'];
	        $post['remind_info'] = '在'.$nowtime."，"."管理员给您取消了订单，订单号为:".$row['payid'];
	        $post['remind_time'] = $nowtime;

        }
        /* 付款*/
        if($state==2){
		    $setdata['order_status'] = 2;
		    $setdata['pay_status'] = 2;
		    $setdata['pay_time'] = 'sysdate()';
	        $sn = '付款，订单id为'.$order_id;
	
	        $maiarray['user_id'] = $row['user_id'];
	        $maiarray['remind_info'] = '在'.$nowtime."，"."管理员将您的订单状态改为支付，订单号为:".$row['payid'];
	        $maiarray['remind_time'] = $nowtime;
	
	        $post['user_id'] = $row['shop_id'];
	        $post['remind_info'] = '在'.$nowtime."，"."管理员将您的订单状态改为支付，订单号为:".$row['payid'];
	        $post['remind_time'] = $nowtime;
        }
        /* 商家发货*/
        if($state==3){
		    $setdata['transport_status'] = 1;		
	        $sn = '商家发货，订单id为'.$order_id;
	
	        $maiarray['user_id'] = $row['user_id'];
	        $maiarray['remind_info'] = '在'.$nowtime."，"."管理员将您的订单状态改为已发货，订单号为:".$row['payid'];
	        $maiarray['remind_time'] = $nowtime;
	
	        $post['user_id'] = $row['shop_id'];
	        $post['remind_info'] = '在'.$nowtime."，"."管理员将您的订单状态改为已发货，订单号为:".$row['payid'];
	        $post['remind_time'] = $nowtime;
        }
        /* 确认收货*/
        if($state==4){
		    $setdata['order_status'] = 3;
	        $sn = '确认收货，订单id为'.$order_id;
	
	        $maiarray['user_id'] = $row['user_id'];
	        $maiarray['remind_info'] = '在'.$nowtime."，"."管理员将您的订单状态改为已收货，订单号为:".$row['payid'];
	        $maiarray['remind_time'] = $nowtime;
	
	        $post['user_id'] = $row['shop_id'];
	        $post['remind_info'] = '在'.$nowtime."，"."管理员将您的订单状态改为已收货，订单号为:".$row['payid'];
	        $post['remind_time'] = $nowtime;
        }
		$query  = array();		
		$query['keyid'] = true;
		$safeData['orderinfo'] = $setdata;				
		$query['set'] = $safeData;	
		$query['conditions'] = array('orderinfo.id'=>$order_id);		
		if(POP::exe('[@@:UM orderinfo][@@:QU save]',$query)) {	
		    //修改订单状态;
			$logarray = array(
				'log_time' => date("Y-m-d H:i:s"),
				'admin_id' => $this->my->id,
			    'admin_name' => $this->my->username,
				'log_info' => $sn,
			    'ip_address' => Request::getclientIp(),					
			);
			$safeData['adminlog'] = $logarray;								
			POP::exe('[@@:UM adminlog][@@:QU save]',$safeData);				
		
			$this->loadModel('remindinfo');	
			//发送给买方
			$safeData['remindinfo'] = $maiarray;								
			POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);	
			//发送给卖方			
	        $safeData['remindinfo'] = $post;								
			POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);	
			
            $this->flash("修改成功！", Router::url('admin/order/editdate?id='.$order_id));	
        } else {
            $this->flash("修改失败！", Router::url('admin/order/editdate?id='.$order_id));	
        }


	}		
	
	public function setstatus() {	
        /* post */
        $post = Request::get_args('order_id');
        $order_ids = implode(",",$post);
        $status = Request::get_args('status');	
		$setdata = array();	
        if($status=='cancel') {
		    $setdata['order_status'] = 0;		
        } elseif($status=='pay') {
		    $setdata['pay_status'] = 1;		
        } elseif($status=='nopay') {
		    $setdata['pay_status'] = 0;			
        } elseif($status=='transport') {
		    $setdata['transport_status'] = 1;			
        } elseif($status=='notransport') {
		    $setdata['transport_status'] = 0;	
        }
		$query  = array();		
		$query['keyid'] = true;
		$safeData['orderinfo'] = $setdata;				
		$query['set'] = $safeData;	
		$query['conditions'] = array('orderinfo.id'=>$order_ids);		
		if(POP::exe('[@@:UM orderinfo][@@:QU save]',$query)) {		
		    //修改订单状态;
			$array = array(
				'log_time' => date("Y-m-d H:i:s"),
				'admin_id' => $this->my->id,
			    'admin_name' => $this->my->username,
				'log_info' => "修改订单状态",
			    'ip_address' => Request::getclientIp(),					
			);
			$safeData['adminlog'] = $logarray;								
			POP::exe('[@@:UM adminlog][@@:QU save]',$safeData);						
	        $this->flash("修改成功！", Request::referer());
        } else {
	        $this->flash("修改失败！", Request::referer());
        }	
	
	}


}

?>